package okna;

import java.awt.EventQueue;
import java.awt.Toolkit;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import java.awt.Color;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.swing.JLabel;

import komorki.ManagerKomorek;
import komorki.Neighbours;

import stany.BinaryState;
import stany.State;
import stany.Tryby;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

import automaty.Automat;



public class Okno {

	private JFrame frame;
	private final float WOLNO = 1.0f;
	private final float SZYBKO = 0.5f;
	private final int oddstep = 30;
	private ManagerKomorek manager;
	private Plansza plansza;
	private PrzyciskPokaz pokazKolor;
	private OpisKoloru kolorOpis;
	private Wymiary wymiary;
	private int currentRow;
	private int currentColumn;
	public int width = 850;
	public int height = 650;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Okno window = new Okno();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 * @throws IOException 
	 */
	public Okno() throws IOException {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 * @throws IOException 
	 */
	private void initialize() throws IOException {
		frame = new JFrame();
		frame.setBounds(100, 100, width, height);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		Zakonczenie zak = new Zakonczenie();
		
		plansza = new Plansza(this,10,10);
		plansza.setBackground(Color.WHITE);
		plansza.setLayout(null);
		frame.getContentPane().add(plansza);
		/*JPanel plansza = new JPanel();
		plansza.setBackground(Color.WHITE);
		plansza.setBounds(20, 45, 510, 330);
		plansza.setLayout(null);
		frame.getContentPane().add(plansza);*/
	
		
		JButton btnLosuj = new JButton(new ImageIcon("resources/gui/dice.png"));
		btnLosuj.setBorder(BorderFactory.createEmptyBorder());
		btnLosuj.setContentAreaFilled(false);
		btnLosuj.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				manager.random();
			}
		});
	
		btnLosuj.setBounds(132, 0, 45, 45);
		frame.getContentPane().add(btnLosuj);
		
		JButton btnCzy = new JButton(new ImageIcon("resources/gui/miotla.png"));
		btnCzy.setBorder(BorderFactory.createEmptyBorder());
		btnCzy.setContentAreaFilled(false);
		btnCzy.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arOpisKolorug0) {
				manager.clean();
			}
		});
		btnCzy.setBounds(196, 0, 45, 45);
		frame.getContentPane().add(btnCzy);
		
		JButton btnSzybko = new JButton(new ImageIcon("resources/gui/fast.png"));
		btnSzybko.setBorder(BorderFactory.createEmptyBorder());
		btnSzybko.setContentAreaFilled(false);
		btnSzybko.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				manager.setSpeed(SZYBKO);
			}
		});
		btnSzybko.setBounds(330, 0, 45, 45);
		frame.getContentPane().add(btnSzybko);
		
		JButton btnWolno = new JButton(new ImageIcon("resources/gui/slow.png"));
		btnWolno.setBorder(BorderFactory.createEmptyBorder());
		btnWolno.setContentAreaFilled(false);
		btnWolno.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				manager.setSpeed(WOLNO);
			}
		});
		btnWolno.setBounds(263, 0, 45, 45);
		frame.getContentPane().add(btnWolno);
		
		
		
		JButton btnKrok = new JButton(new ImageIcon("resources/gui/krok.png"));
		btnKrok.setBorder(BorderFactory.createEmptyBorder());
		btnKrok.setContentAreaFilled(false);
		btnKrok.setBounds(408, 0, 45, 45);
		btnKrok.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				manager.setKrok(true);
				
			}
		});
		frame.getContentPane().add(btnKrok);
		
		
		//dynamiczne dodawanie modulow do comobox
		StringBuilder sb = new StringBuilder();
		for(Tryby tryb : Tryby.values()){
			sb.append(tryb.toString() + ",");
		}
		//;sb.toString().split(",")
		JComboBox<String> comboBox = new JComboBox<String>(sb.toString().split(","));
		comboBox.setBounds(20, 12, 100, 20);
		comboBox.addItemListener(new Zamiana());
		
		frame.getContentPane().add(comboBox);
		
		JButton btnZakocz = new JButton(new ImageIcon("resources/gui/quit.png"));
		btnZakocz.setBorder(BorderFactory.createEmptyBorder());
		btnZakocz.setContentAreaFilled(false);
		btnZakocz.addActionListener(zak);
		btnZakocz.setBounds(558, 0, 45, 45);
		frame.getContentPane().add(btnZakocz);
		
		JButton btnStop = new JButton(new ImageIcon("resources/gui/stop.png"));
		btnStop.setBorder(BorderFactory.createEmptyBorder());
		btnStop.setContentAreaFilled(false);
		btnStop.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				manager.stop();
			}
		});
		btnStop.setBounds(487, 0, 45, 45);
		frame.getContentPane().add(btnStop);
		
		//trzeba ustawic na starcie kolory i opisy na starcie bo nie bedzie ich widac
		State s = new BinaryState();
		pokazKolor = new PrzyciskPokaz(558, 152);
		pokazKolor.setLocation(615, 12);
		pokazKolor.setBackground(s.getAvailableColors()[0]);
		frame.getContentPane().add(pokazKolor);
		
		kolorOpis = new OpisKoloru(597,152,s.getDescriptions()[0]);
		kolorOpis.setLocation(663, 10);
		frame.getContentPane().add(kolorOpis);
		
		JMenuBar menuBar = new JMenuBar();
		frame.setJMenuBar(menuBar);
		
		JMenu mnPlik = new JMenu("plik");
		menuBar.add(mnPlik);
		
		JMenuItem mntmZmieWymiarPlanszy = new JMenuItem("Zmień wymiary planszy");
		mntmZmieWymiarPlanszy.addActionListener(new Wymiar());
		mnPlik.add(mntmZmieWymiarPlanszy);
		
		JMenuItem mntmZakocz = new JMenuItem("Zakończ");
		mntmZakocz.addActionListener(zak);
		mnPlik.add(mntmZakocz);
		
		JMenu mnPomoc = new JMenu("pomoc");
		menuBar.add(mnPomoc);
		
		JMenuItem mntmOProgramie = new JMenuItem("o programie");
		mntmOProgramie.addActionListener(new Info());
		mnPomoc.add(mntmOProgramie);
		
		manager = ManagerKomorek.manager(this.plansza);
		manager.buildNeighbourhoodStruct();
		//zeby zamknac dzialanie programu jak ktos nacisnie exit
		frame.addWindowListener(new Lokno(manager));	
		
	}
	
	public void changeSize(){
		manager.stop();
		//mozliwe ze tutaj jest blad z tym warunkiem i nie zmieni wartosci np 10x20
		//tragiczny blad  bo potrzebuje managera zanim powstanie
		if(ManagerKomorek.maxRow != currentRow || ManagerKomorek.maxColumn != currentColumn){
			//plansza = new Plansza(this, ManagerKomorek.maxRow, ManagerKomorek.maxColumn);
			Automat au = manager.getAutomat();
			
			manager = ManagerKomorek.manager(plansza);
			
			Neighbours.changeManager(manager);
			manager.buildNeighbourhoodStruct();
			plansza.changeSize(ManagerKomorek.maxRow, ManagerKomorek.maxColumn);
			manager.setAutomat(au.getClone(ManagerKomorek.maxRow, ManagerKomorek.maxColumn, manager.getKomorki()));
			manager.clean();
			plansza.repaint();
		}
	}
	
	public void changeColor(Color c, String desc){
		pokazKolor.setBackground(c);
		kolorOpis.setText(desc);
	}
	
	class Zamiana implements ItemListener{

		@Override
		public void itemStateChanged(ItemEvent e) {
			if(e.getStateChange() == ItemEvent.SELECTED){
				manager.changeAutomat(e.getItem().toString());
			}
		}
		
	}
	
	class Wymiar implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			currentColumn = ManagerKomorek.maxColumn;
			currentRow = ManagerKomorek.maxRow;
			
			wymiary = new Wymiary(Okno.this);
			wymiary.setVisible(true);
			
		}
		
	}
	
	class Zakonczenie implements ActionListener{

		public void actionPerformed(ActionEvent arg0) {
			WindowEvent wev = new WindowEvent(frame, WindowEvent.WINDOW_CLOSING);
            Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(wev);
		}
		
	}
	
}


class Lokno extends WindowAdapter{
	
 private ManagerKomorek man;
	public Lokno(ManagerKomorek man){
		this.man = man;
	}
	
	@Override
	public void windowClosing(WindowEvent e) {
		man.stop();
	}
}

class Info implements ActionListener{

	@Override
	public void actionPerformed(ActionEvent e) {
		Informacje dialog = new Informacje();
		dialog.setVisible(true);
		
	}
	
}

